<?php
namespace app\admin\controller;
use think\Db;
//社区管理
class Community extends Base{
	//小区的列表
	public function plot_list(){
		$tit	=!empty(input('tit')) ? input('tit') : '';
		$map['del']=['NEQ',1];	//没有绑定商品
		
		if(!empty($tit)){
			$map['tit']=['like','%'.$tit.'%'];	//根据生成批次筛选
		} 
		
    	$rel=Db::name('plot')
		->where($map)
		->order('ctime desc,id desc')
		->paginate(10,false,['query'=>[
			'tit'		=>$tit
		]]);
		$this->assign('data',$rel);
		$this->assign('page',$rel->render());
		$this->assign('empty',"<tr><td class='empty' colspan='10'>当前没有数据 </td></tr>");
		$this->assign(['tit'=>$tit]);
		return $this->fetch();
	}
	//编辑小区
	public function edit_plot(){
		$id=input('id');
		if(request()->isGet()){
			if(!$id){				//新建，渲染页面
				return $this->fetch();
			}else{					//修改，渲染页面
				$plot=Db::name('plot')->where('id',$id)->find();
				return $this->fetch('',['plot'=>$plot]);
			}
		}else{
			$data=request()->post();
			$insert=[
				'head_img' 	=> $data['head_img'],
				'tit'		=> $data['tit'],
				'decl'		=> $data['decl'],
				'lng'		=> $data['lng'],
				'lat'		=> $data['lat'],
				'address'	=> $data['address'],
				'ctime'		=> time()
			];
			if(!$id){			//提交新增商品数据
				$rel=Db::name('plot')->insert($insert);
				if($rel>0){
					return rel(1,'添加成功');
				}else{
					return rel(-1,'添加失败');
				}
			}else{
				$rel=Db::name('plot')->where('id',$id)->update($insert);
				if($rel>0){
					return rel(1,'修改成功');
				}else{
					return rel(-1,'修改失败');
				}
			}
		}
	}
	//房源列表
	public function house_list(){
		
	}
	//物业列表
	public function steward_list(){
		$name		=!empty(input('name')) ? input('name') : '';
		$phone		=!empty(input('phone')) ? input('phone') : '';
		$map['del']=['NEQ',1];
		
		if(!empty($name)){
			$map['name']=['like','%'.$name.'%'];
		} 
		if(!empty($phone)){
			$map['phone']=['like','%'.$phone.'%'];
		}
    	$rel=Db::name('steward')
		->where($map)
		->order('id desc')
		->paginate(3,false,['query'=>[
			'phone'		=>$phone,
			'name'		=>$name
		]]);
		$this->assign('data',$rel);
		$this->assign('page',$rel->render());
		$this->assign('empty',"<tr><td class='empty' colspan='10'>当前没有数据 </td></tr>");
		$this->assign(['name'=>$name,'phone'=>$phone]);
		return $this->fetch();
	}
	//编辑物业
	public function edit_steward(){
		$id=input('id');
		if(request()->isGet()){
			if(!$id){				//新建，渲染页面
				return $this->fetch();
			}else{					//修改，渲染页面
				$steward=Db::name('steward')->where('id',$id)->find();
				return $this->fetch('',['steward'=>$steward]);
			}
		}else{
			$data=request()->post();
			$insert=[
				'head_img' 	=> $data['head_img'],
				'name'		=> $data['name'],
				'decl'		=> $data['decl'],
				'phone'		=> $data['phone']
			];
			if(!$id){			//提交新增商品数据
				$rel=Db::name('steward')->insert($insert);
				if($rel>0){
					return rel(1,'添加成功');
				}else{
					return rel(-1,'添加失败');
				}
			}else{
				$rel=Db::name('steward')->where('id',$id)->update($insert);
				if($rel>0){
					return rel(1,'修改成功');
				}else{
					return rel(-1,'修改失败');
				}
			}
		}
	}
	//第三方服务列表
	public function serve_list(){
		$company_name=!empty(input('company_name')) ? input('company_name') : '';
		$phone		=!empty(input('phone')) ? input('phone') : '';
		$type		=!empty(input('type')) ? input('type') : '';
		
		$map['del']=['NEQ',1];
		
		if(!empty($company_name)){
			$map['company_name']=['like','%'.$company_name.'%'];
		} 
		if(!empty($phone)){
			$map['phone']=['like','%'.$phone.'%'];
		}
		if(!empty($type)){
			$map['type']=['EQ',$type];	//根据生成批次筛选
		} 
    	$rel=Db::name('serve')
		->where($map)
		->order('id desc')
		->paginate(3,false,['query'=>[
			'phone'			=>$phone,
			'company_name'	=>$company_name,
			'type'			=>$type
		]]);
		$cates=config('admin.serve');
		array_shift($cates);
		$this->assign('data',$rel);
		$this->assign('page',$rel->render());
		$this->assign('empty',"<tr><td class='empty' colspan='10'>当前没有数据 </td></tr>");
		$this->assign('cates',$cates);
		$this->assign(['company_name'=>$company_name,'phone'=>$phone,'type'=>$type]);
		return $this->fetch();
	}
	//编辑家政服务
	public function edit_serve(){
		$id=input('id');
		if(request()->isGet()){
			$cates=config('admin.serve');
			array_shift($cates);
			$this->assign('cates',$cates);
			if(!$id){				//新建，渲染页面
				return $this->fetch();
			}else{					//修改，渲染页面
				$serve=Db::name('serve')->where('id',$id)->find();
				return $this->fetch('',['serve'=>$serve]);
			}
		}else{
			$data=request()->post();
			$insert=[
				'company_name'=>$data['company_name'],
				'head_img' 	=> $data['head_img'],
				'name'		=> $data['name'],
				'decl'		=> $data['decl'],
				'phone'		=> $data['phone'],
				'price'		=> $data['price']
			];
			if(!$id){			//提交新增商品数据
				$insert['type']=!empty($data['type']) ? $data['type'] : 0;
				$rel=Db::name('serve')->insert($insert);
				if($rel>0){
					return rel(1,'添加成功');
				}else{
					return rel(-1,'添加失败');
				}
			}else{
				$rel=Db::name('serve')->where('id',$id)->update($insert);
				if($rel>0){
					return rel(1,'修改成功');
				}else{
					return rel(-1,'修改失败');
				}
			}
		}
	}
}
